﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class NewUser : Form
    {
        private UserDao user;
        public NewUser()
        {
            InitializeComponent();
            user = new UserDao();
            dtgCustomers.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSkyBlue;
            dtgCustomers.GridColor = Color.BurlyWood;
            dtgCustomers.EnableHeadersVisualStyles = false;
            this.BackColor = Color.LightCyan;
        }

        private void NewUser_Load(object sender, EventArgs e)
        {
            onload();
        }

        private void onload()
        {
            txtFullName.Text = "";
            txtUserName.Text = "";
            txtCornfirmPass.Text = "";
            txtNewPass.Text = "";
            cbxAdmin.Checked = false;
            DataTable table = user.getUsers("");
            dtgCustomers.Rows.Clear();
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    int i = dtgCustomers.Rows.Add();
                    dtgCustomers.Rows[i].Cells[0].Value = i + 1;
                    dtgCustomers.Rows[i].Cells[1].Value = row1["AD_FLG"].ToString().Equals("T") ? row1["USR_NAME"].ToString() + "(*)" : row1["USR_NAME"].ToString();
                    dtgCustomers.Rows[i].Cells[2].Value = row1["FULL_NAME"].ToString();
                    dtgCustomers.Rows[i].Cells[4].Value = row1["ID"].ToString();
                    if (i % 2 == 1)
                    {
                        dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
        }

        private bool check()
        {
            if (txtFullName.Text.Trim() == ""
                || txtCornfirmPass.Text.Trim() == ""
                || txtNewPass.Text.Trim() == ""
                || txtUserName.Text.Trim() == "")
            {
                return false;
            }
            return true;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (check())
            {
                if (!txtNewPass.Text.Trim().Equals(txtCornfirmPass.Text.Trim()))
                {
                    Error er = new Error(UserConstant.USER_CONFIRM_PASS);
                    er.ShowDialog();
                    er.Dispose();
                }
                else
                {
                    string admin = "";
                    if (cbxAdmin.Checked)
                    {
                        admin = "T";
                    }
                    else
                    {
                        admin = "F";
                    }


                    if (user.insertUser(txtUserName.Text.Trim(), txtNewPass.Text.Trim(), txtFullName.Text.Trim(), admin))
                    {
                        Result r = new Result(UserConstant.USER_ADD_SUC);
                        r.ShowDialog();
                        r.Dispose();
                        onload();
                    }
                    else
                    {
                        Error er = new Error(UserConstant.USER_ADD_FAIL);
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
            }
            else
            {
                Error er = new Error(UserConstant.USER_INPUT);
                er.ShowDialog();
                er.Dispose();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            DataTable table = user.getUsers(txtName.Text.Trim());
            dtgCustomers.Rows.Clear();
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    int i = dtgCustomers.Rows.Add();
                    dtgCustomers.Rows[i].Cells[0].Value = i + 1;
                    dtgCustomers.Rows[i].Cells[1].Value = row1["USR_NAME"].ToString();
                    dtgCustomers.Rows[i].Cells[2].Value = row1["FULL_NAME"].ToString();
                    dtgCustomers.Rows[i].Cells[4].Value = row1["ID"].ToString();
                    if (i % 2 == 1)
                    {
                        dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
               
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtNewPass.Text = "";
            txtFullName.Text = "";
            txtUserName.Text = "";
            txtCornfirmPass.Text = "";
        }

        private void dtgCustomers_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 3)
            {
                int idUsr = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[4].Value.ToString());
                ChangePassForUser change = new ChangePassForUser(idUsr);
                change.ShowDialog();
                change.Dispose();
                onload();
            }
            if (e.RowIndex >= 0 && e.ColumnIndex == 5)
            {
                int idUsr = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[4].Value.ToString());
                if (idUsr == LoginDao.userID)
                {
                    Error er = new Error("Bạn không thể xóa chính mình ra khỏi hệ thống");
                    er.ShowDialog();
                    er.Dispose();
                }
                else
                {
                    string fullName = dtgCustomers.Rows[e.RowIndex].Cells[2].Value.ToString();
                    Confirm con = new Confirm(UserConstant.USER_DEL + fullName);
                    con.ShowDialog();
                    if (con.IsAccept)
                    {
                        if (user.deleteUser(idUsr))
                        {
                            Result r = new Result(UserConstant.USR_DEL_SUC);
                            r.ShowDialog();
                            r.Dispose();
                            onload();
                        }
                        else
                        {
                            Error er = new Error(UserConstant.USR_DEL_FAIL);
                            er.ShowDialog();
                            er.Dispose();
                        }
                    }
                    change.Dispose();
                }
            }
        }
    }
}
